/**
 * 客户列表
*/

var wawa_inventory = new Object({
    table:null,
    init:function(){
        if(null != this.table)
            return;
        //console.info("init ok...")
        this.queryInventory();
        this.bindEvent();
    },
    queryInventory:function(){
        this.table = $('#tb_wawa_inventory').bootstrapTable({
            url: contextPath + '/wawa/inventory/listData.json',
            responseHandler: function (res) {
                return res.data;
            },
            queryParams: function (params) {
                return params;
            },
            pagination: true,
            pageList: '[10,20, 50, 100]',
            //showColumns: true,
            showRefresh: true,
            showToggle: true,
            //showPaginationSwitch:true,
            sidePagination: 'server',
            //striped:true,
            sortable: false,
            height: 700,
            search: true,
            searchAlign: 'left',
            toolbar: '<div style="float: left;margin-right:5px;">' +
            '    <button class="btn btn-default" type="button" onclick="wawa_inventory.add()">' +
            '        <i style="margin-right:2px;" class="fa fa-search"></i>' +
            '        <span>新增</span>' +
            '    </button>' +
            '</div>',
            idField: 'id',
            uniqueId: 'id',
            //singleSelect: true,
            clickToSelect: true,
            columns: [{
                field: 'id',
                title: 'ID',
                width: 100,
                visible:false
            }, {
                field: 'code',
                title: '娃娃编号',
                width: 100
            }, {
                field: 'name',
                title: '娃娃名称',
                width: 100
            }, {
                field: 'waWaType.typeName',
                title: '娃娃类型',
                width: 100
            }, {
                field: 'thumbnailUrl',
                title: '缩略图',
                width: 100,
                formatter:function(value,row,index){
                    return '<img src="' + value + '" height="30" width="30" />';
                }
            }, {
                field: 'bigPicUrl',
                title: '大图地址',
                width: 100,
                formatter:function(value,row,index){
                    return '<a href="'+value+'" target="_blank">查看</a>'
                }
            }, {
                field: 'inventory',
                title: '库存数',
                width: 100
            }, {
                field: 'description',
                title: '描述',
                width: 100
            },{
                field: 'op',
                title: '操作',
                align: 'center',
                width: 100,
                formatter: function (value, row, index) {
                    var strHtml = '';
                    strHtml = '<div class="btn-group">' +
                    '<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">更多<i class="fa fa-caret-down"></i></button>' +
                    '   <ul class="dropdown-menu">'+
                    '       <li><a href="javascript:void(0);" onclick="wawa_inventory.editWaWa('+row.id+')">编辑</a></li>'+
                    '       <li><a href="javascript:void(0);" onclick="wawa_inventory.incomingWaWa('+row.id+')">入库</a></li>'+
                    '       <li><a href="javascript:void(0);" onclick="wawa_inventory.adjustWaWa('+row.id+')">调整</a></li>'+
                    '       <li><a href="javascript:void(0);" onclick="wawa_inventory.deleteWaWa('+row.id+')">删除</a></li>'+
                    '   </ul>'+
                    '</div>'
                    return strHtml;
                }
            }]
        });
        $('.search input').attr("placeholder", "名称");
    },
    editWaWa:function(id){
        var wawa_res='';
        $.get({
            url:contextPath+'/wawa/inventory/findById/'+id,
            async:false,
            success:function(res){
                wawa_res = res.data;
            }
        })

        var type_list = '';
        $.get({
            url:contextPath+'/wawa/type/findAll',
            async:false,
            success:function(res){
                type_list = res.data;
            }
        })
        var editWaWaModal = '<div id="editWaWaModal" class="modal fade" tabindex="-1" role="dialog" style="display: none;">'+
            '   <div class="modal-dialog">'+
            '       <div class="modal-content">'+
            '           <div class="modal-header">'+
            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'+
            '               <h4 class="modal-title" id="editWaWaModalLabel">创建娃娃库存</h4>'+
            '           </div>'+
            '           <div class="modal-body">'+
            '               <form class="form-horizontal" id="editWaWaForm">'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">娃娃名称：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="hidden" class="form-control" name="id" value="'+wawa_res.id+'" />'+
            '                           <input type="text" class="form-control" name="name" value="'+wawa_res.name+'" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">库存数：</label>'+
            '                       <div class="col-sm-9">'+
            wawa_res.inventory+'个'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">娃娃类型：</label>'+
            '                       <div class="col-sm-9">' +
            '                           <select name="wawaTypeId" class="form-control">';
        var select_list = '';
        $(type_list).each(function(index){
            var t = type_list[index];
            if(wawa_res.wawaTypeId == t.id) {
                select_list += '<option value="' + t.id + '" selected="selected">' + t.typeName + '</option>';
            }else{
                select_list += '<option value="' + t.id + '">' + t.typeName + '</option>';
            }
        })
        editWaWaModal += select_list;
        editWaWaModal +='                </select>' +
            '                       </div>' +
            '                   </div>';

        editWaWaModal+='    <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">描述：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="description" value="'+wawa_res.description+'" />'+
            '                       </div>'+
            '                   </div>'+

            '                   <input type="hidden" name="bigPicUrl" id="ww_pic_url" value="'+wawa_res.bigPicUrl+'"/>'+
            '                   <input type="hidden" name="thumbnailUrl" id="ww_thumbnail_url" value="'+wawa_res.thumbnailUrl+'"/>'+
            '               </form>' +
            '               <div class=" form-horizontal">'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">图片：</label>'+
            '                       <div class="col-sm-9" style="padding-top:7px;">' +
            '                           <input type="file" id="upload_ww_img"  accept="image/*" name="upload" class="file" data-min-file-count="1"/>' +
            '                           <div class="dropzone-box" style="text-align:center">';
        if(null == wawa_res.thumbnailUrl || undefined == wawa_res.thumbnailUrl){
            editWaWaModal += '<img src="'+wawa_inventory.kongbai_img+'" id="wawa_inventory_img" style="width:100%;height:100%"/>';
        }else{
            editWaWaModal += '<img src="'+wawa_res.thumbnailUrl+'" id="wawa_inventory_img" style="width:100%;height:100%"/>';
        }

        editWaWaModal +=    '        </div>' +
            '                   </div>' +
            '               </div>'+
            '           </div>' +
            '           <div class="modal-footer">' +
            '               <center>' +
            '                   <button class="btn btn-primary" onclick="wawa_inventory.editSubmit()" >保存</button>' +
            '                   <button class="btn btn-primary" type="button" data-dismiss="modal" aria-hidden="true">关闭</button>' +
            '               </center>' +
            '           </div>'+
            '       </div>'+
            '   </div>'+
            '</div>';

        //console.info(editWaWaModal);
        /**
         * 添加html中，隐藏
         */
        document.body.appendChild($(editWaWaModal)[0]);
        $('#editWaWaModal').on('hidden.bs.modal', function () {
            document.body.removeChild($('#editWaWaModal')[0]);
        });

        //$('#upload_img').pixelFileInput({ placeholder: '没有选择文件...' });

        wawa_inventory.bindEvent();

        //显示
        $("#editWaWaModal").modal('show')
    },
    adjustWaWa:function(id){
        var wawa_res = '';
        $.get({
            url:contextPath+'/wawa/inventory/findById/'+id,
            async:false,
            success:function(res){
                wawa_res = res.data;
            }
        })

        var adjustWaWaModal = '<div id="adjustWaWaModal" class="modal fade" tabindex="-1" role="dialog" style="display: none;">'+
            '   <div class="modal-dialog">'+
            '       <div class="modal-content">'+
            '           <div class="modal-header">'+
            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'+
            '               <h4 class="modal-title">库存调整</h4>'+
            '           </div>'+
            '           <div class="modal-body">'+
            '               <form class="form-horizontal" id="adjustWaWaForm">'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">娃娃名称：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="hidden" class="form-control" name="waWaId" value="'+wawa_res.id+'" />'+
            '                           <input type="hidden" class="form-control" name="waWaName" value="'+wawa_res.name+'" />'+
            '                           <input type="hidden" class="form-control" name="waWaType" value="'+wawa_res.waWaType.typeName+'" />'+
            '                           <input type="hidden" class="form-control" name="adjustBefore" value="'+wawa_res.inventory+'" />'+
                                        wawa_res.name+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">库存数：</label>'+
            '                       <div class="col-sm-9">'+
            wawa_res.inventory+'个'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">调整后：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="adjustAfter" value="" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">调整说明：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="remark" value="" />'+
            '                       </div>'+
            '                   </div>'+
            '               </form>' +
            '           </div>' +
            '           <div class="modal-footer">' +
            '               <center>' +
            '                   <button class="btn btn-primary" onclick="wawa_inventory.adjustWaWaSubmit()" >保存</button>' +
            '                   <button class="btn btn-primary" type="button" data-dismiss="modal" aria-hidden="true">关闭</button>' +
            '               </center>' +
            '           </div>'+
            '       </div>'+
            '   </div>'+
            '</div>';

        //console.info(adjustWaWaModal);
        /**
         * 添加html中，隐藏
         */
        document.body.appendChild($(adjustWaWaModal)[0]);
        $('#adjustWaWaModal').on('hidden.bs.modal', function () {
            document.body.removeChild($('#adjustWaWaModal')[0]);
        });
        //显示
        $("#adjustWaWaModal").modal('show')
    },
    adjustWaWaSubmit:function(){
        $.ajax({
            url:contextPath+'/wawa/inventory/adjust',
            data:$('#adjustWaWaForm').serialize(),
            method:'post',
            success:function(res){
                if(res.meta.success){
                    wawa_inventory.table.bootstrapTable('refresh');
                    $('#adjustWaWaModal').modal('hide')
                    alert_message(res.data);
                }else{
                    alert_message(res.meta.message);
                }
            },
            error:function(){

            }
        })
    },
    editSubmit:function(){
        $.ajax({
            url:contextPath+'/wawa/inventory/update',
            method:'post',
            data:$('#editWaWaForm').serialize(),
            success:function(res){
                if(res.meta.success) {
                    wawa_inventory.table.bootstrapTable('refresh');
                    $('#editWaWaModal').modal('hide');
                    alert_message(res.data);
                }else{
                    alert_message(res.meta.message);
                }
            },
            error: function () {
                alert_message("添加出错，请联系管理员")
            }
        });
    },
    bindEvent: function () {
        $('#upload_ww_img').on('clearInput',function(e){
            $("#wawa_inventory_img").attr("src",wawa_inventory.kongbai_img)
        })

        $('#upload_ww_img').die().live('change',function(){
            $.ajaxFileUpload({
                url:contextPath+'/file/upload',
                secureuri:false,
                fileElementId:'upload_ww_img',//file标签的id
                dataType: 'json',//返回数据的类型
                //data:{name:'logan'},//一同上传的数据
                success: function (res, status) {
                    //把图片替换
                    $("#wawa_inventory_img").attr("src",res.data.file_thumbnail_url)
                    $('#ww_pic_url').attr('value',res.data.file_url);
                    $('#ww_thumbnail_url').attr('value',res.data.file_thumbnail_url);
                    if(typeof(res.error) != 'undefined') {
                        if(res.error != '') {
                            alert(res.error);
                        } else {
                            alert(res.msg);
                        }
                    }
                },
                error: function (data, status, e) {
                    alert(e);
                }
            });
        })
    },
    kongbai_img:'http://static-wwj.ballq.cn/upload/images/kongbai.png',
    add:function(){
        var type_list = '';
        $.get({
            url:contextPath+'/wawa/type/findAll',
            async:false,
            success:function(res){
                $(res.data).each(function(index){
                    var t = res.data[index];
                    type_list +='<option value="'+ t.id+'">'+ t.typeName+'</option>';
                });
            }
        })
        var addWaWaModal = '<div id="addWaWaModal" class="modal fade" tabindex="-1" role="dialog" style="display: none;">'+
            '   <div class="modal-dialog">'+
            '       <div class="modal-content">'+
            '           <div class="modal-header">'+
            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'+
            '               <h4 class="modal-title" id="addWaWaModalLabel">创建娃娃库存</h4>'+
            '           </div>'+
            '           <div class="modal-body">'+
            '               <form class="form-horizontal" id="addWaWaForm">'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">娃娃名称：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="name" value="" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">娃娃类型：</label>'+
            '                       <div class="col-sm-9">' +
            '                           <select name="wawaTypeId" class="form-control">' +
            '                               <option value="-1" selected="selected">请选择娃娃类型</option>';

        addWaWaModal += type_list;
        addWaWaModal +='                </select>' +
            '                       </div>' +
            '                   </div>'

            addWaWaModal+='    <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">描述：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="description" value="" />'+
            '                       </div>'+
            '                   </div>'+

            '                   <input type="hidden" name="bigPicUrl" id="ww_pic_url" value=""/>'+
            '                   <input type="hidden" name="thumbnailUrl" id="ww_thumbnail_url" value=""/>'+
            '               </form>'+
            '               <div class="form-group">'+
            '                   <label class="col-sm-3 control-label">图片：</label>'+
            '                   <div class="col-sm-9" style="padding-top:7px;">' +
            '                       <input type="file" id="upload_ww_img"  accept="image/*" name="upload" class="file" data-min-file-count="1"/>' +
            '                       <div class="dropzone-box" style="text-align:center">' +
            '                           <img src="'+wawa_inventory.kongbai_img+'" id="wawa_inventory_img" style="width:100%;height:100%"/>'+
            '                      </div>' +
            '               </div>'+
            '           </div>' +
            '           <div class="modal-footer">' +
            '               <center>' +
            '                   <button class="btn btn-primary" onclick="wawa_inventory.addSubmit()" >保存</button>' +
            '                   <button class="btn btn-primary" type="button" data-dismiss="modal" aria-hidden="true">关闭</button>' +
            '               </center>' +
            '           </div>'+
            '       </div>'+
            '   </div>'+
            '</div>';

        //console.info(addWaWaModal);
        /**
         * 添加html中，隐藏
         */
        document.body.appendChild($(addWaWaModal)[0]);
        $('#addWaWaModal').on('hidden.bs.modal', function () {
            document.body.removeChild($('#addWaWaModal')[0]);
        });

        //$('#upload_img').pixelFileInput({ placeholder: '没有选择文件...' });

        wawa_inventory.bindEvent();

        //显示
        $("#addWaWaModal").modal('show')
    },
    addSubmit:function(){
        $.ajax({
            url:contextPath+'/wawa/inventory/add',
            method:'post',
            data:$('#addWaWaForm').serialize(),
            success:function(res){
                if(res.meta.success) {
                    wawa_inventory.table.bootstrapTable('refresh');
                    $('#addWaWaModal').modal('hide');
                    alert_message(res.data);
                }else{
                    alert_message(res.meta.message);
                }
            },
            error: function () {
                alert_message("添加出错，请联系管理员")
            }
        });

    },
    incomingWaWa:function(id){
        var res = "";
        $.ajax({
            url:contextPath+'/wawa/inventory/findById/'+id,
            method:'get',
            async:false,
            success:function(data){
                //获取数据t
                res = data;
            },
            error:function(){
                alert_message('请求错误，请联系管理员');
            }
        });
        var incomingWaWaModal = '<div id="incomingWaWaModal" class="modal fade" tabindex="-1" role="dialog" style="display: none;">'+
            '   <div class="modal-dialog">'+
            '       <div class="modal-content">'+
            '           <div class="modal-header">'+
            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'+
            '               <h4 class="modal-title" id="incomingWaWaModalLabel">'+res.data.name+'-入库</h4>'+
            '           </div>'+
            '           <div class="modal-body">'+
            '               <form class="form-horizontal" id="incomingWaWaForm">'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">娃娃名称：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="hidden" name="id" value="'+res.data.id+'" />'+
                                        res.data.name+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">库存数：</label>'+
            '                       <div class="col-sm-9">'+
                                            res.data.inventory+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">入库数：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="inventory" value="" />'+
            '                       </div>'+
            '                   </div>'+
            '               </form>'+
            '           </div>' +
            '           <div class="modal-footer">' +
            '               <center>' +
            '                   <button class="btn btn-primary" onclick="wawa_inventory.incomingSubmit()" >保存</button>' +
            '                   <button class="btn btn-primary" type="button" data-dismiss="modal" aria-hidden="true">关闭</button>' +
            '               </center>' +
            '           </div>'+
            '       </div>'+
            '   </div>'+
            '</div>';

        //alert(editWaWaTypeModal);

        //显示
        /**
         * 添加html中，隐藏
         */
        document.body.appendChild($(incomingWaWaModal)[0]);
        $('#incomingWaWaModal').on('hidden.bs.modal', function () {
            document.body.removeChild($('#incomingWaWaModal')[0]);
        });

        //显示
        $('#incomingWaWaModal').modal('show');


    },
    incomingSubmit:function(){
        $.ajax({
            url:contextPath+'/wawa/inventory/incoming',
            data:$('#incomingWaWaForm').serialize(),
            method:'post',
            success:function(res){
                if(res.meta.success) {
                    alert_message("入库成功");
                    wawa_inventory.table.bootstrapTable('refresh');
                    $('#incomingWaWaModal').modal('hide');
                }else{
                    alert_message(res.meta.message);
                }
            },
            error:function(){
                alert_message("修改出错，请联系管理员");
            }
        });
    },
    deleteWaWa:function(id){
        bootbox.confirm('确认要删除娃娃吗？',function(result){
                if(result) {
                    $.ajax({
                        url: contextPath + '/wawa/inventory/deleteById/' ,
                        method: 'post',
                        data:{id:id},
                        success: function (res) {
                            wawa_inventory.table.bootstrapTable('refresh');
                            if(res.meta.success){
                                alert_message(res.data);
                            }else{
                                alert_message(res.meta.message);
                            }
                        },
                        error: function () {
                            alert_message("删除出错，请联系管理员");
                        }
                    });
                }
            }
        );
    }

});